﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TaskRuleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITaskRuleDetailDA taskDetailRuleDA = DataAccess.CreateTaskRuleDetailDA();
        private static readonly ITaskRuleDA taskRuleDA = DataAccess.CreateTaskRuleDA();

        public TaskRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public TaskRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TaskRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<TaskRuleInfo> taskRuleInfos = new List<TaskRuleInfo>();
                TaskRuleInfo taskRuleInfo = null;
                foreach (string id in IDS)
                {
                    taskRuleInfo = taskRuleDA.GetTaskRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    taskRuleInfo.TaskRuleDetailInfo = taskDetailRuleDA.GetTaskRuleDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    taskRuleInfos.Add(taskRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                taskDetailRuleDA.DeleteTaskRuleDetail(dataBase, tran, IDS, this.dataBaseID);
                taskRuleDA.DeleteTaskRule(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<TaskRuleInfo>(dataBase, tran, taskRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_RULE", this.dataBaseID[0]);
                    if ((taskRuleInfos[i].TaskRuleDetailInfo != null) && (taskRuleInfos[i].TaskRuleDetailInfo.Count > 0))
                    {
                        for (int j = 0; j < taskRuleInfos[i].TaskRuleDetailInfo.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<TaskRuleDetailInfo>(dataBase, tran, taskRuleInfos[i].TaskRuleDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return taskRuleDA.GetAllTaskRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string taskRuleID)
        {
            TaskRuleInfo taskRule = taskRuleDA.GetTaskRuleByID(taskRuleID, this.dataBaseID[0]);
            if (taskRule != null)
            {
                taskRule.TaskRuleDetailInfo = taskDetailRuleDA.GetTaskRuleDetailListByID(taskRuleID, this.dataBaseID[0], this.pubParasInfo);
            }
            return taskRule;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            TaskRuleQueryEntity taskRule = obj as TaskRuleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(taskRule.TaskRuleID))
            {
                sqlWhere.Add(" TASK_RULE_ID " + taskRule.TaskRuleID);
            }
            if (!string.IsNullOrEmpty(taskRule.Descr))
            {
                sqlWhere.Add(" DESCR " + taskRule.Descr);
            }
            if (!string.IsNullOrEmpty(taskRule.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + taskRule.IsActive);
            }
            return taskRuleDA.GetTaskRuleByQueryList(sqlWhere, taskRule, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string taskRuleID = string.Empty;
            try
            {
                TaskRuleInfo taskRule = obj as TaskRuleInfo;
                if (taskRuleDA.CheckTaskRuleIDUnique(dataBase, tran, taskRule.TaskRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F6050_002", this.pubParasInfo.UserLanguage, new List<string> { taskRule.TaskRuleID });
                }
                taskRule.UpdatedBy = taskRule.CreatedBy;
                taskRule.CreatedDate = DateTime.Now;
                taskRule.UpdatedDate = DateTime.Now;
                taskRuleDA.InsertTaskRule(dataBase, tran, taskRule, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<TaskRuleInfo>(dataBase, tran, null, taskRule, AuditAction.Insert, this.pubParasInfo.UserID, "TASK_RULE", this.dataBaseID[0]);
                taskRuleID = taskRule.TaskRuleID;
                if ((taskRule.TaskRuleDetailInfo != null) && (taskRule.TaskRuleDetailInfo.Count > 0))
                {
                    for (int i = 0; i < taskRule.TaskRuleDetailInfo.Count; i++)
                    {
                        if (taskDetailRuleDA.CheckTaskRuleDetailIDUnique(dataBase, tran, taskRule.TaskRuleDetailInfo[i].TaskRuleID, taskRule.TaskRuleDetailInfo[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F6050_003", this.pubParasInfo.UserLanguage, new List<string> { taskRule.TaskRuleDetailInfo[i].TaskRuleID, taskRule.TaskRuleDetailInfo[i].LineID.ToString() });
                        }
                        else
                        {
                            taskRule.TaskRuleDetailInfo[i].CreatedDate = DateTime.Now;
                            taskRule.TaskRuleDetailInfo[i].UpdatedDate = DateTime.Now;
                            taskRule.TaskRuleDetailInfo[i].CreatedBy = this.pubParasInfo.UserID;
                            taskRule.TaskRuleDetailInfo[i].UpdatedBy = this.pubParasInfo.UserID;
                            taskDetailRuleDA.InsertTaskRuleDetail(dataBase, tran, taskRule.TaskRuleDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<TaskRuleDetailInfo>(dataBase, tran, null, taskRule.TaskRuleDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "TASK_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return taskRuleID;
        }

        public string Update(object obj)
        {
            string taskRuleID = string.Empty;
            try
            {
                TaskRuleInfo taskRule = obj as TaskRuleInfo;
                TaskRuleInfo oldTaskRuleInfo = taskRuleDA.GetTaskRuleByID(taskRule.TaskRuleID, this.dataBaseID[0]);
                if (taskRuleDA.UpdateTaskRule(taskRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TaskRuleInfo>(oldTaskRuleInfo, taskRule, AuditAction.Update, this.pubParasInfo.UserID, "TASK_RULE", this.dataBaseID[0]);
                    taskRuleID = taskRule.TaskRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return taskRuleID;
        }
    }
}

